package com.example.intermingle.util;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.*;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 绿松石
 * @version V1.0
 * @Title commonUtils
 * @Description  公共方法类
 * @date 2021年09月03日 09:02
 * @since V1.0
 */
public class CommonUtils {
    /**
     * 获取当前用户
     * @return
     */
    public static String getName(){
        //获取当前用户名
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String name=auth.getName();
        return name;
    }

    /**
     * 对密码加密
     * @param userPassword
     * @return
     */
    public static String encryptionPassWord(String userPassword){
        //定义一个springSecurity密码加密对象
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String userPasswordEncoder=encoder.encode(userPassword);
        return  userPasswordEncoder;
    }

    /**
     * 对密码进行解密
     * @param UserPassword 页面传过来的密码
     * @param encoderPassword  数据库的密码
     * @return
     */
    public static Boolean decryptPassWord(String UserPassword,String encoderPassword) {
        //定义一个springSecurity密码加密对象
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        //对密码进行解密
        Boolean result=encoder.matches(UserPassword, encoderPassword);

        return result;

    }

    /**
     * 判断对象是否为空
     *
     * @param obj
     * @return boolean
     */
    public static boolean isNull(Object obj) {
        if (null == obj) {
            return true;
        }
        if (obj instanceof CharSequence) {
            return ((CharSequence) obj).length() == 0;
        }
        if (obj instanceof Collection) {
            return ((Collection<?>) obj).isEmpty();
        }
        if (obj instanceof Map) {
            return ((Map<?, ?>) obj).isEmpty();
        }
        if (obj instanceof Object[]) {
            Object[] object = (Object[]) obj;
            if (object.length == 0) {
                return true;
            }
            boolean empty = true;
            for (int i = 0; i < object.length; i++) {
                if (!isNull(object[i])) {
                    empty = false;
                    break;
                }
            }
            return empty;
        }
        return false;
    }

    /**
     * 判断list集合是否为null
     * @param list
     * @return
     */
    public static boolean isEmpty(List<?> list) {
        if (list == null || list.size() < 1) {
            return true;
        }
        return false;
    }

    /**
     * 判断对象是否为空
     *
     * @param obj
     * @return boolean
     */
    public static boolean isNotNull(Object obj) {
        return !isNull(obj);
    }

    /**
     * 关闭资源
     * @param closable
     */
    public static void close(Closeable closable) {
        if (closable != null) {
            try {
                closable.close();
            } catch (IOException e) {
                //LOGGER.error(e.getMessage(), e);
            }
        }
    }

    /**
     * 转换Date为指定格式的字符串
     *
     * @return String
     */
    public static String convertDateToString(Date date, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }

    /**
     * 将Bean转成Map
     *
     * @param obj
     * @return Map
     */
    public static Map<String, Object> bean2Map(Object obj) {
        if (obj == null) {
            return null;
        }
        HashMap<String, Object> map = new HashMap<String, Object>();
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();
                if (!key.equals("class")) { // 过滤class属性
                    Method getter = property.getReadMethod(); // 得到property对应的getter方法
                    Object obj2 = getter.invoke(obj);
                    map.put(key, obj2);
                }
            }
        } catch(Exception e) {
           // LOGGER.error(e.getMessage(), e);
        }
        return map;
    }

    public static  Date Date(){

        return Timestamp.valueOf(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.SIMPLIFIED_CHINESE).format(new Date()));
    }

}
